package com.uci.scheduleshare.adapters;

import java.util.List;

import com.uci.scheduleshare.DBApplication;
import com.uci.scheduleshare.R;
import com.uci.scheduleshare.adapters.ColorPickerDialog.OnColorChangedListener;
import com.uci.scheduleshare.calendar.Contact;
import com.uci.scheduleshare.calendardb.CalendarDatabase;

import android.R.anim;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
 * CS 190 Spring 2012
 * ContactsAdapter.java
 * The Adapter for displaying all of the contacts
 * 
 * @author Patrick Hsu, Lauren Ko, Pomi Park
 *
 */
public class ContactsAdapter extends BaseAdapter {
	
	private List<Contact> contacts;
	private Context context;
	private CalendarDatabase db;
	private ColorPickerDialog cpd;
	private SharedPreferences myPrefs;
	
	public ContactsAdapter(List<Contact> contacts, Context context, CalendarDatabase db, SharedPreferences myPrefs) {
		super();
		this.contacts = contacts;
		this.context = context;
		this.db = db;
		this.myPrefs = myPrefs;
	}
	
	@Override
	public int getCount() {
		return contacts.size();
	}

	@Override
	public Object getItem(int position) {
		return contacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if(convertView == null) { // if the view is null, inflate a new view
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.contacts_row, parent, false);
			holder = new ViewHolder(); // Create a viewholder for holding all the XML objects
			holder.username = (TextView) convertView.findViewById(R.id.contact_name);
			holder.calendarName = (TextView) convertView.findViewById(R.id.contact_calendar_name);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.contact_check);
			holder.colorPicker = (ImageView) convertView.findViewById(R.id.color_picker);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		String name = contacts.get(position).getName();
		String calendarName = contacts.get(position).getCalendarName();
		int color = contacts.get(position).getColor();
		
		holder.username.setText(name);
		holder.calendarName.setText(calendarName);
		holder.colorPicker.setBackgroundColor(color);
		holder.colorPicker.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				OnColorChangedListener colorChangedListener = new OnColorChangedListener(){
					@Override
					public void colorChanged(int color) {
						// After a color is picked, change the color value in the contact's object 
						// and in the CalendarDatabase
						if(contacts.get(position).isMe()) {
							SharedPreferences.Editor editor = myPrefs.edit();
							editor.putInt("color", color);
							editor.commit();
						} else {
							db.setColor(contacts.get(position).getID(), color);
						}
						contacts.get(position).setColor(color);
						holder.colorPicker.setBackgroundColor(color);
					}
				};
				
				cpd = new ColorPickerDialog(context, colorChangedListener, contacts.get(position).getColor());
				cpd.show();
			}
		});

		holder.checkBox.setChecked(contacts.get(position).isChecked());

		holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// If the checkbox is checked, then change the isChecked value in the contact's object
				contacts.get(position).setChecked(isChecked);
			}
		});
		return convertView;
	}
	
	static class ViewHolder{
		TextView username;
		TextView calendarName;
		CheckBox checkBox;
		ImageView colorPicker;
	}

}
